/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.panelsManager;
import java.awt.FlowLayout;
import java.awt.Window;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JPanel;
import researchgrants.parts.LoggedData.panels.events.SaveValidationEvent;
import researchgrants.parts.LoggedData.panels.events.SaveValidationHandler;
import researchgrants.parts.LoggedData.panels.events.SaveValidationListener;
import researchgrants.parts.panels.Panels;




/**
 *
 * @author DOStudent1
 */
public abstract class PanelsManager extends JPanel implements SaveValidationListener, SaveValidationHandler{

    
    public PanelsManager() {
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
    }

    public abstract void startAsNew(boolean isNew);
    
    public abstract void display();
    
    protected void updateDisplay() {
        revalidate();
        if (getTopLevelAncestor() instanceof Window) {
            ((Window) getTopLevelAncestor()).pack();
        }
        if (panels != null) {
            if (panels.getParentWindow() != null) {
                panels.getParentWindow().pack();
            }
        }
    }
    
    Panels panels;
    public void setPanels(Panels panels) {
        this.panels=panels;
        panels.addSaveValidationListener(this);
    }

    public Panels getPanels() {
        return(panels);
    }


    public abstract void setMandatory(boolean isMandatory);

    private Collection<SaveValidationListener> saveListeners = new LinkedList<SaveValidationListener>();

    @Override
    public void addSaveValidationListener(SaveValidationListener saveValidationListener) {
        saveListeners.add(saveValidationListener);
    }

    protected void notifyAllSaveValidationListeners(SaveValidationEvent e) {
        for (Iterator<SaveValidationListener> it = saveListeners.iterator(); it.hasNext();) {
            SaveValidationListener saveValidatorListener = it.next();
            saveValidatorListener.saveValidationSaved(e);
        }
    }
    
    @Override
    public void saveValidationSaved(SaveValidationEvent e){
        for (Iterator<SaveValidationListener> it = saveListeners.iterator(); it.hasNext();) {
            SaveValidationListener saveValidatorListener = it.next();
            saveValidatorListener.saveValidationSaved(e);
        }
    }
    
}
